package dk.snaveponyklub.persist.impl;

import java.io.CharArrayReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;

import java.net.URL;
import java.net.URLConnection;

import javax.naming.StringRefAddr;

import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import dk.snaveponyklub.handler.impl.HandlerImpl;

public class XMLLoaderImpl {
	
	public void writePersons(Document document) {
		
		writeDocument(document, "person.xml");
	}

	public void writeTimeslots(Document document) {
		
		writeDocument(document, "timeslot.xml");
	}

	public void writeHorses(Document document) {
		
		writeDocument(document, "horse.xml");
	}

	private void writeDocument(Document document, String filename) {
		try {
			URL fileUrl = getClass().getClassLoader().getResource(filename);
			File file = new File(fileUrl.toURI());
	        XMLOutputter xo = new XMLOutputter();
	        FileWriter fw = new FileWriter(file);
	        xo.output(document, fw);
	        fw.flush();
	        fw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public Document loadPersonsDocument() {
        return loadDocument("person.xml", "Persist.xsd");
	}

	public Document loadClassesDocument() {
        return loadDocument("class.xml", "Persist.xsd");
	}
	
	private Document loadDocument(String filename, String xsdFilename) {		
		SAXBuilder builder = new SAXBuilder();
        try {
			URL fileUrl = getClass().getClassLoader().getResource(filename);
			File file = new File(fileUrl.toURI());
			// Do validation against schema
	        URL schemaFileUrl = getClass().getClassLoader().getResource(xsdFilename);
			File schemaLocation = new File(schemaFileUrl.toURI());			
			builder.setValidation(true);
			builder.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
			builder.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", schemaLocation);
	        Document document = builder.build(file);
            return document;
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
	}
	
	public Document validate(String xml, String xsdFilename) {	
		SAXBuilder builder = new SAXBuilder();
        try {
			URL tmpFileUrl = getClass().getClassLoader().getResource("temp.xml");
			File tmpFile = new File(tmpFileUrl.toURI());
			FileWriter fw = new FileWriter(tmpFile);
			fw.write(xml);
			fw.flush();
			fw.close();
        	
			URL fileUrl = getClass().getClassLoader().getResource("temp.xml");
			File file = new File(fileUrl.toURI());
	        URL schemaFileUrl = getClass().getClassLoader().getResource(xsdFilename);
			File schemaLocation = new File(schemaFileUrl.toURI());
			builder.setValidation(true);
			builder.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
			builder.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", schemaLocation);
	        Document document = builder.build(file);
            return document;
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
	}

	public Document loadXml(String xml) {	
		SAXBuilder builder = new SAXBuilder();
        try {
	        Document document = builder.build(new CharArrayReader(xml.toCharArray()));
            return document;
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
	}

	public Document loadHorsesDocument() {
        return loadDocument("horse.xml", "Persist.xsd");
	}
	
	public Document loadTimeslotsDocument() {
        return loadDocument("timeslot.xml", "Persist.xsd");
	}

	public static void main(String[] args) throws IOException {
//        Document document = new XMLLoaderImpl().loadHorsesDocument();
//        Document document = new XMLLoaderImpl().loadPersonsDocument();
//        String xml = new HandlerImpl().extractXML(document);
//        System.out.println("Xml: " + xml);
//        System.out.println("Validate: " + new XMLLoaderImpl().validate(xml, "Persist.xsd"));
		
    	//Element timeslot = (new FilePersistImpl()).getTimeSlots(1);
        String xml = "<?xml version='1.0' encoding='UTF-8'?><s:persist xmlns:s='http://www.snaveponyklub.dk'><s:timeslots><s:timeslot endtime='2010-10-05T10:00:00' starttime='2010-10-05T11:00:00' id='4'></s:timeslot></s:timeslots></s:persist>";
		Document document = new XMLLoaderImpl().loadXml(xml);
        XMLOutputter xo = new XMLOutputter();
        xo.output(document.getRootElement(), System.out);
    }	
}
